
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author David
 */
public class ServerConfig {
    private Integer port;
    private HashMap<String,String> aliasMap;

    public ServerConfig(){
        this.aliasMap = new HashMap<String,String>();
    }

    public ServerConfig(String path){
        this.loadConfig(path);
    }
    public Integer getPort(){
        return this.port;
    }

    public String resolveAlias(String alias){
        if(this.aliasMap.containsKey(alias)){
            return this.aliasMap.get(alias);
        }
        return "";
    }

    public void loadConfig(String path){
        try{
            FileInputStream file = new FileInputStream(path);
            DataInputStream data = new DataInputStream(file);
            BufferedReader contents = new BufferedReader(new InputStreamReader(data));
            String line;
            StringTokenizer options;
            String option, value;
            while ((line = contents.readLine()) != null)   {
                options = new StringTokenizer(line);
                option = options.nextToken();
                if(option.equals("Alias")){
                    option = options.nextToken();
                    value = options.nextToken();
                    this.aliasMap.put(option,value);
                    System.out.println(option+"===>"+value);
                }else if(option.equals("Listen")){
                    this.port = new Integer(options.nextToken());
                    System.out.println("Port: "+this.port);
                }
            }
            file.close();
        }catch (Exception e){
          System.err.println("Error: " + e.getMessage());
        }
    }
}
